package com.open.capacity.reimburse.controller;

import java.util.Map;

import com.open.capacity.commons.Result;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.security.access.prepost.PreAuthorize;
import com.open.capacity.reimburse.entity.PbdBudgetInit;
import com.open.capacity.reimburse.service.PbdBudgetInitService;

import com.open.capacity.commons.PageResult;

import javax.validation.Valid;

/**
 * @author
 * @email
 * @date 2021-06-29 10:26:53
 */
@RestController
@RequestMapping("pbdbudgetinits")
@Api(tags = "")
public class PbdBudgetInitController {

    @Autowired
    private PbdBudgetInitService pbdBudgetInitService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @PreAuthorize("hasAnyAuthority('pbd_budget_init:pbdbudgetinit:list')")
    public PageResult list(@RequestParam Map<String, Object> params) {
        PageResult pageResult = pbdBudgetInitService.pageAll(params);
        return pageResult;
    }

    /**
     * 查询单条记录
     */
    @RequestMapping("/findById")
    @PreAuthorize("hasAnyAuthority('pbd_budget_init:pbdbudgetinit:list')")
    public Result findById(String id) {
        return Result.succeed(pbdBudgetInitService.findById(id));
    }

    /**
     * 查询单条记录
     */
    @RequestMapping("/findByYear")
    @PreAuthorize("hasAnyAuthority('pbd_budget_init:pbdbudgetinit:list')")
    public Result findByYear(Integer year) {
        return Result.succeed(pbdBudgetInitService.findByYear(year));
    }


    /**
     * 保存
     */
    @RequestMapping("/saveOrUpdate")
    @PreAuthorize("hasAnyAuthority('pbd_budget_init:pbdbudgetinit:save')")
    public Result save(@RequestBody @Valid PbdBudgetInit pbdBudgetInit, String keyValue) {
        if (StringUtils.isEmpty(keyValue)) {
            pbdBudgetInitService.save(pbdBudgetInit);
        } else {
            pbdBudgetInit.setId(keyValue);
            pbdBudgetInitService.update(pbdBudgetInit);
        }
        return Result.succeed("保存成功");
    }


    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    @PreAuthorize("hasAnyAuthority('pbd_budget_init:pbdbudgetinit:delete')")
    public Result delete(@PathVariable String id) {
        pbdBudgetInitService.delete(id);
        return Result.succeed("删除成功");
    }

}
